﻿/*-------------------------------------------------------------------------
* 命名空间: XrayDirection.ViewModels/EapViewModel 
* 类   名: EapViewModel
* 功   能: N/A
* 时   间: 2024-08-26 11:29:32
* 版   本: V1.0
* 作   者: Fioman
* 格   言: Talk is cheap,show me the code ^_^
*-------------------------------------------------------------------------*/
using Prism.Commands;
using Prism.Mvvm;
using SiliconSelect.Common;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Net.Http;
using System.Windows;
using XrayDirection.Common;
using XrayDirection.Common.EnumVal;
using XrayDirection.DAL.DataEntity;
using XrayDirection.DAL.LocalDataAccess;
using XrayDirection.Models;

namespace XrayDirection.ViewModels
{
    public class EapViewModel : BindableBase
    {
        public EapViewModel()
        {
            RefreshEapParams();
            InitMockRequestList();
            MockRequestCommand = new DelegateCommand(DoMockRequest);
            SaveEapParams = new DelegateCommand(DoSaveEapParams);
        }

        private void DoSaveEapParams()
        {
            var eapParams = EapParamList[0];
            EapParamsEntity newEntity = new EapParamsEntity()
            {
                Id = eapParams.Id,
                MachineCode = eapParams.MachineCode,
                DeviceIp = eapParams.DeviceIp,
                DevicePort = eapParams.DevicePort,
                EapIp = eapParams.EapIp,
                EapPort = eapParams.EapPort,
                HeartBreakTime = eapParams.HeartBreakTime,
                EapFixedURL = eapParams.EapFixedURL
            };
            // 保存Eap的参数
            var result = EapParamsAccess.UpdateEapParams(newEntity);
            if (result.State)
            {
                MessageBox.Show("保存成功");
            }
            else
            {
                MessageBox.Show($"保存失败: {result.Message}");
            }
            RefreshEapParams();
        }

        public DelegateCommand SaveEapParams { get; private set; }


        private async void DoMockRequest()
        {
            string methodName = MockRequestList[MockRequestIndex];
            HttpResponseMessage? response;
            switch (methodName)
            {
                case "DeviceHeartbeat":
                    response = await RequestHelper.RequestDeviceHeartbeatAsync(methodName);
                    RequestHelper.ParseNormalResponse(methodName, response);
                    break;
                case "DeviceAlarmReport":
                    string alarmCode = "AlarmInfo01";
                    string alarmDes = "居中机构电机出现了故障";
                    int alarmLevel = 3;
                    int alarmStaus = 1;
                    response = await RequestHelper.RequestDeviceAlarmReport(methodName, alarmCode, alarmDes, alarmLevel, alarmStaus);
                    RequestHelper.ParseNormalResponse(methodName, response);
                    break;
                case "DeviceStatusReport":
                    response = await RequestHelper.RequestDeviceStatusReport(methodName, (int)StatusCode.运行, StatusCode.运行.ToString());
                    RequestHelper.ParseNormalResponse(methodName, response);
                    break;
                case "DeviceCraftReportExt":
                    response = await RequestHelper.RequestDeviceCraftReportExt(methodName);
                    RequestHelper.ParseNormalResponse(methodName, response);
                    break;
                case "EqpClearFinish":
                    response = await RequestHelper.RequestEqpClearFinish(methodName);
                    RequestHelper.ParseNormalResponse(methodName, response);
                    break;
                case "askDownTaskEvent":
                    response = await RequestHelper.RequestAskDownTaskEvent(methodName);
                    RequestHelper.ParseNormalResponse(methodName, response);
                    break;
            }
        }

        /// <summary>
        /// 初始化模拟请求的Request名称
        /// </summary>
        private void InitMockRequestList()
        {
            MockRequestList = new List<string>
            {
                "DeviceHeartbeat",
                "DeviceAlarmReport",
                "DeviceStatusReport",
                "DeviceCraftReportExt",
                "EqpClearFinish",
                "askDownTaskEvent"
            };
            MockRequestIndex = 0;
        }

        private List<string> _mockRequestList = null!;

        public List<string> MockRequestList
        {
            get { return _mockRequestList; }
            set
            {
                _mockRequestList = value;
                RaisePropertyChanged();
            }
        }

        private int _mockRequestIndex;

        public int MockRequestIndex
        {
            get { return _mockRequestIndex; }
            set
            {
                _mockRequestIndex = value;
                RaisePropertyChanged();
            }
        }

        private string? _requestResponse;

        public string? RequestResponse
        {
            get { return _requestResponse; }
            set
            {
                _requestResponse = value;
                RaisePropertyChanged();
            }
        }


        private string? _responseStatus;

        public string? ResponseStatus
        {
            get { return _responseStatus; }
            set
            {
                _responseStatus = value;
                RaisePropertyChanged();
            }
        }

        private string? _responseMsg;

        public string? ResponseMsg
        {
            get { return _responseMsg; }
            set
            {
                _responseMsg = value;
                RaisePropertyChanged();
            }
        }


        private ObservableCollection<EapParamsModel> _eapParamList = new ObservableCollection<EapParamsModel>();

        public ObservableCollection<EapParamsModel> EapParamList
        {
            get { return _eapParamList; }
            set
            {
                _eapParamList = value;
                RaisePropertyChanged();
            }
        }

        private bool _eapIsOnline = false;

        public bool EapIsOnline
        {
            get { return _eapIsOnline; }
            set
            {
                _eapIsOnline = value;
                RaisePropertyChanged();
            }
        }


        /// <summary>
        /// 刷新EapParams参数
        /// </summary>
        private void RefreshEapParams()
        {
            var result = EapParamsAccess.GetEapParams();
            if (result.State)
            {
                if (result.Data!.Count == 0)
                {
                    // 如果没有数据,就创建一条默认数据进去
                    EapParamsEntity eapParams = new EapParamsEntity()
                    {
                        MachineCode = "HdXray",
                        DeviceIp = "127.0.0.1",
                        DevicePort = 7028,
                        EapIp = "127.0.0.1",
                        EapPort = 7028,
                        HeartBreakTime = 120,
                        EapFixedURL = "http://127.0.0.1:7028/"
                    };
                    var addRes = EapParamsAccess.AddEapParams(eapParams);
                    if (!addRes.State)
                    {
                        LogHelper.WriteLog("数据库操作异常" + result.Message!);
                    }
                    EapParamsModel model = new EapParamsModel()
                    {
                        Id = eapParams.Id,
                        MachineCode = eapParams.MachineCode,
                        DeviceIp = eapParams.DeviceIp,
                        DevicePort = eapParams.DevicePort,
                        EapIp = eapParams.EapIp,
                        EapPort = eapParams.EapPort,
                        HeartBreakTime = eapParams.HeartBreakTime,
                        EapFixedURL = eapParams.EapFixedURL
                    };
                    // 注意Add和Append的区别,Add,是在原序列上添加,而Append是创建一个副本在末尾添加,然后返回新序列
                    EapParamList.Add(model);
                }
                else
                {
                    EapParamList.Clear();
                    foreach (var param in result.Data)
                    {
                        EapParamsModel model = new EapParamsModel()
                        {
                            Id = param.Id,
                            MachineCode = param.MachineCode,
                            DeviceIp = param.DeviceIp,
                            DevicePort = param.DevicePort,
                            EapIp = param.EapIp,
                            EapPort = param.EapPort,
                            HeartBreakTime = param.HeartBreakTime,
                            EapFixedURL = param.EapFixedURL
                        };
                        EapParamList.Add(model);
                    }
                }
                GlobalValues.MachineCode = EapParamList[0].MachineCode;
                GlobalValues.EapFixedUrl = EapParamList[0].EapFixedURL;
            }
        }

        private object? _dataSendToEap;
        public object? DataSendToEap
        {
            get { return _dataSendToEap; }
            set
            {
                _dataSendToEap = value;
                RaisePropertyChanged();
            }
        }

        private object? _dataReceiveFromEap;

        public object? DataReceiveFromEap
        {
            get { return _dataReceiveFromEap; }
            set
            {
                _dataReceiveFromEap = value;
                RaisePropertyChanged();
            }
        }

        public DelegateCommand MockRequestCommand { get; private set; }
    }
}
